<?php

namespace App\Api\Services;

use App\Api\Models\MaterielTypeModel;
use App\Api\Models\OverflowBreakagePackageContentModel;
use App\Api\Models\StorageModel;
use App\Models\CheckModel;
use App\Models\OverflowBreakageChecksModel;
use App\Models\OverflowBreakageDetailModel;
use App\Models\OverflowBreakageModel;
use App\Services\StorageManageService;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class CheckService
{
    static public function agreeOrRefund($data)
    {
        DB::beginTransaction();

        try {
            $overflowBreakageChecksModel = OverflowBreakageChecksModel::where(['o_b_main_id'=>$data['all_id'],'admin_id'=>$data['user_id']])->first();

            if($overflowBreakageChecksModel['status'] != 0) {
                return ['code' => 1, 'msg' => '不可重复操作', 'data' => ''];
            }
            if($data['status']) {
                return SELF::agree($data, $overflowBreakageChecksModel);
            }
            else {
                //更新审核人审核状态
                $overflowBreakageChecksModel->status = 2;
                $overflowBreakageChecksModel->refund_remark = isset($data['remark'])?$data['remark']:"";
                $overflowBreakageChecksModel->save();
                $obc = OverflowBreakageChecksModel::where(['status'=>0,'o_b_main_id'=>$data['all_id']])->first();
                if(!$obc){
                    OverflowBreakageModel::where('id',$data['all_id'])->update(['status'=>2]);//更新报溢报损审核状态
                    CheckModel::where('id',$data['id'])->update(['status'=>1]);//审批状态更新
                }
            }

            //更新临时审批人
            $model = CheckModel::where('id',$data['id'])->first();
            if($model['check_person_tmp']){
                $arr = explode(',',$model['check_person_tmp']);
                $userIdIndex = array_search($data['user_id'],$arr);
                unset($arr[$userIdIndex]);
                $model->check_person_tmp = implode(',',$arr);
                $model->save();
            }

            DB::commit();
            return ['code' => 0, 'msg' => 'success', 'data' => ''];
        }
        catch(\Exception $e) {
            DB::rollBack();
            Log::debug('报溢报损审批:'.var_export($e->getMessage(), true));
            return ['code' => 1, 'msg' => $e->getMessage(), 'data' => ''];
        }
    }

    /**
     *
     * @param $data
     * @param $overflowBreakageChecksModel
     * AUTHOR  : Sun Wu
     * DateTime: 2022/5/20 17:33
     * @return array|bool
     */
    static private function agree($data, $overflowBreakageChecksModel)
    {
        DB::beginTransaction();

        try {
            //更新审核人审核状态
            $overflowBreakageChecksModel->status = 1;
            $overflowBreakageChecksModel->save();
            $all = OverflowBreakageChecksModel::where(['o_b_main_id'=>$data['all_id']])->count();
            $agree = OverflowBreakageChecksModel::where(['status'=>1,'o_b_main_id'=>$data['all_id']])->count();
            $refund = OverflowBreakageChecksModel::where(['status'=>2,'o_b_main_id'=>$data['all_id']])->count();
            if($all==$agree){//全部通过入库
                OverflowBreakageModel::where('id',$data['all_id'])->update(['status'=>1]);//更新报溢报损审核状态
                CheckModel::where('id',$data['id'])->update(['status'=>1]);//审批状态更新

                //出入库参数
                $paramsArr = [];//出入库参数
                $paramsArr['all_id'] = $data['all_id'];
                $paramsArr['remark'] = '';
                $paramsArr['all_id_type'] = 1;//报溢报损均为1
                $OverflowBreakageDetailModel = OverflowBreakageDetailModel::where('o_b_main_id',$data['all_id'])->get()->toArray();
                if(!empty($OverflowBreakageDetailModel)) {
                    foreach ($OverflowBreakageDetailModel as $k=>$val){
                        $paramsArr['content'][$k]['name'] = $val['name'];
                        $paramsArr['content'][$k]['price'] = 0;
                        $paramsArr['content'][$k]['type_name'] = $val['type_name'];
                        $paramsArr['content'][$k]['num'] = $val['num'];
                        $paramsArr['content'][$k]['warehouse'] = $val['warehouse'];
                        $paramsArr['content'][$k]['warehouse_id'] = $val['warehouse_id'];
                    }
                }

                $checkModel = CheckModel::find($data['id']);
                if($checkModel->type==0){//报溢入库
                    $paramsArr['in_storage_time'] = date('Y-m-d H:i:s');
                    $res = StorageManageService::inStorage($data['user_id'], $data['main_id'], $paramsArr);
                    if($res['code']){
                        return ['code' => 1, 'msg' => $res['msg'], 'data' => ''];
                    }
                }else{//报损出库
                    $overflowBreakagePackageContentInfo = OverflowBreakagePackageContentModel::where('o_b_id',$data['all_id'])
                        ->get()->toArray();

                    if(!empty($overflowBreakagePackageContentInfo)) {
                        foreach($overflowBreakagePackageContentInfo as $key => $value) {
                            $materielInfo = MaterielTypeModel::where('id', $value['type_id'])
                                ->first();

                            $storageInfo = StorageModel::where('id', $value['storage_id'])
                                ->first();
                            $paramsArr['content'][$key]['name'] = $materielInfo['materiel_name'];
                            $paramsArr['content'][$key]['price'] = 0;
                            $paramsArr['content'][$key]['type_name'] = $materielInfo['type_name'];
                            $paramsArr['content'][$key]['num'] = $value['total_num'];
                            $paramsArr['content'][$key]['warehouse'] = $storageInfo['storage_name'];
                            $paramsArr['content'][$key]['warehouse_id'] = $storageInfo['id'];
                        }
                    }

                    $paramsArr['out_storage_time'] = date('Y-m-d H:i:s');
                    $res = StorageManageService::outStorage($data['user_id'], $data['main_id'], $paramsArr);
                    if($res['code']){
                        return ['code' => 1, 'msg' => $res['msg'], 'data' => ''];
                    }
                }
            }

            if($all==($agree+$refund) && $refund){//部分拒绝
                echo 1;die;
                OverflowBreakageModel::where('id',$data['all_id'])->update(['status'=>2]);//更新报溢报损审核状态
                CheckModel::where('id',$data['id'])->update(['status'=>1]);//审批状态更新
            }
            else {
                echo 2;die;
            }

            DB::commit();
            return ['code' => 0, 'msg' => 'success', 'data' => ''];
        }
        catch (\Exception $e) {
            DB::rollBack();
            Log::debug('报溢报损审批:'.var_export($e->getMessage(), true));
            return ['code' => 1, 'msg' => $e->getMessage(), 'data' => ''];
        }
    }
}
